﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Partial Class nfaq
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub



    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Dim blnAddNew As Boolean

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intID As Int16
        If Not Page.IsPostBack Then
            hideText.Text = -1
            blnAddNew = True
            Button1.Text = "Thêm mới"
            If IsNumeric(Request.QueryString("id")) Then
                intID = Request.QueryString("id")
                BindGrid(intID)
                blnAddNew = False
                Button1.Text = "Cập nhật"
            End If
            Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
            Dim sSql As String = "select idnn,ngonngu from ngonngu"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            idnn.DataSource = myDataSet2
            idnn.DataBind()
            sSql = "select idnhomfaq,convert(varchar(3),  (Case when  isnull(idvn,0) = 0 then idnhomfaq else idvn end)) + '-' + tennhomfaq  as  tennhomfaq  from dmnhomfaq order by idnn,thutu"
            Dim myDataSet3 As New DataSet
            Dim myAdapter2 As New SqlDataAdapter(sSql, myConn)
            myAdapter2.Fill(myDataSet3, sSql)
            cboChude.DataValueField = "idnhomfaq"
            cboChude.DataTextField = "tennhomfaq"
            cboChude.DataSource = myDataSet3
            cboChude.DataBind()
            myConn.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sSql As String
        If hideText.Text <= 0 Then
            sSql = "insert into faq (cauhoi,idnn,TT,thutu,idnhomfaq,tieude_faq,tukhoa_faq,mota_faq) " & _
                    "values(@cauhoi,@idnn,@TT,@thutu,@idnhomfaq,@tieude,@tukhoa,@mota)"
            blnAddNew = True
        Else
            sSql = "update faq set" & _
            " cauhoi=@cauhoi,idnn=@idnn, " & _
            " tieude_faq=@tieude,tukhoa_faq=@tukhoa, " & _
            " mota_faq=@mota," & _
            " TT=@TT,thutu=@thutu,idnhomfaq=@idnhomfaq" & _
            " where idfaq=@idfaq"
            blnAddNew = False
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idfaq", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@cauhoi", cauhoi.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tieude", tieude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tukhoa", tukhoa.Text))
        MyCommand.Parameters.Add(New SqlParameter("@mota", mota.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", TT.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@idnn", idnn.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@thutu", CInt(Thutu.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@idnhomfaq", cboChude.SelectedValue))
        MyCommand.ExecuteNonQuery()
        If blnAddNew Then
            sSql = "select max(idfaq) from faq"
            MyCommand.CommandText = sSql
            hideText.Text = MyCommand.ExecuteScalar
        End If
        sSql = "update faq set traloi =N'" & Replace(traloi.Text, "'", "''") & "' where idfaq =" & hideText.Text
        MyCommand.CommandText = sSql
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect("dmfaq.aspx")
    End Sub

    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select top 1 idfaq,cauhoi,idnn,TT,thutu,traloi,idnhomfaq, tieude_faq, tukhoa_faq, mota_faq from faq where idfaq = " & vId & " order by thutu"
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            cauhoi.Text = myDr("cauhoi")
            tieude.Text = myDr("tieude_faq")
            tukhoa.Text = myDr("tukhoa_faq")
            mota.Text = myDr("mota_faq")
            TT.Checked = myDr("TT")
            Thutu.Text = myDr("thutu")
            traloi.Text = myDr("traloi")
            idnn.SelectedValue = myDr("idnn")
            hideText.Text = myDr("idfaq")
            cboChude.SelectedValue = myDr("idnhomfaq")
        End If
        myConn.Close()
    End Sub

End Class


